<%@page import="java.util.Date"%>
<%@page import="com.nescafe.nescafepromo.util.FormatUtil"%>
<%@page import="com.nescafe.nescafepromo.command.CommandType"%>
<%@page import="com.nescafe.nescafepromo.entity.Purchase"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="java.util.List"%>
<%@page import="com.nescafe.nescafepromo.model.NPResponse"%>
<%@page import="com.nescafe.nescafepromo.controller.AdminController"%>
<%@page import="com.nescafe.nescafepromo.model.NPRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String now = FormatUtil.formatDate(new Date());
	NPRequest npReq = new NPRequest(request,CommandType.SEARCH_PURCHASES);
	if(!request.getParameterMap().containsKey("email")){
		npReq.addParam("email", null);
	}
	if(!request.getParameterMap().containsKey("to")){
		npReq.addParam("to", now);
	}
	if(!request.getParameterMap().containsKey("from")){
		npReq.addParam("from", now);
	}
	if(!request.getParameterMap().containsKey("branch")){
		npReq.addParam("branch", "0");
	}
	
	
	AdminController ctr = new AdminController();
	NPResponse npRes = ctr.process(npReq);
	List<Entity> result = npRes.getResults();
%>
<div class="table-container">
	<table border="1" cellpadding="5px">
		<tr>
			<th>Receipt No</th>
			<th>Date Purchased</th>
			<th>Branch</th>
			<th>Facebook Email</th>
			<th>Amount (Php)</th>
			<th>Points</th>
		</tr>
		<%
			String receiptNo = null;
			String email = null;
			String amount = null;
			String points = null;

			for (Entity entity : result) {
				Purchase purchase = new Purchase(entity);
				receiptNo = purchase.getReceiptNo();				
				email = purchase.getEmail();
				amount = FormatUtil.amountFormat.format(purchase.getAmount());
				points = FormatUtil.totalPointsFormat.format(purchase.getPoints());
		%>
		<tr>
			<td><%=receiptNo%></td>
			<td><%=purchase.getDisplayReceiptDate()%></td>
			<td><%=purchase.getBranchName()%></td>
			<td><%=email%></td>
			<td><%=amount%></td>
			<th><%=points%></th>
		</tr>
		<%
			}
		%>

		<!-- 
		<tr>
			<td>39343434</td>
			<td>March 20, 2012 01:45 PM</td>
			<td>Gaisano Grand Mandaue</td>
			<td>customer@yahoo.com</td>
			<td>3,000.00</td>
			<th>300</th>
		</tr>
		<tr>
			<td>39343434</td>
			<td>March 20, 2012 01:45 PM</td>
			<td>Gaisano Grand Mandaue</td>
			<td>customer@yahoo.com</td>
			<td>3,000.00</td>
			<th>300</th>
		</tr>
		<tr>
			<td>39343434</td>
			<td>March 20, 2012 01:45 PM</td>
			<td>Gaisano Grand Mandaue</td>
			<td>customer@yahoo.com</td>
			<td>3,000.00</td>
			<th>300</th>
		</tr>
		<tr>
			<td>456454545</td>
			<td>January 5, 2012 01:45 PM</td>
			<td>Gaisano Grand Cebu</td>
			<td>customer@yahoo.com</td>
			<td>10,000.00</td>
			<th>1,000</th>
		</tr>
		 -->
	</table>
</div>
